package cn.itcalvin.sncoal.cth.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.itcalvin.sncoal.cth.domain.InfoMine;
import cn.itcalvin.sncoal.cth.pojo.common.RespCommon;
import cn.itcalvin.sncoal.cth.pojo.common.RespUserInfo;
import cn.itcalvin.sncoal.cth.pojo.req.mine.ReqMineModify;
import cn.itcalvin.sncoal.cth.pojo.response.mine.RespMine;
import cn.itcalvin.sncoal.cth.utils.JwtUtil;
import cn.itcalvin.vo.Message;
import cn.itcalvin.vo.MessageUtil;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import cn.itcalvin.sncoal.cth.mapper.main.InfoMineMapper;
import cn.itcalvin.sncoal.cth.service.InfoMineService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class InfoMineServiceImpl implements InfoMineService{

    @Autowired
    private InfoMineMapper infoMineMapper;

    /**
     * 下拉框中的矿区矿井
     *
     * @return
     */
    @Override
    public Message<List<RespCommon>> getAllMineListBySelect(Long mineAreaId) {
        final List<InfoMine> allList = infoMineMapper.selectByExample(Example.builder(InfoMine.class).where(Sqls.custom().andEqualTo("isDelete", false)).orderByDesc("id").build());
        List<RespCommon> collect;
        if(mineAreaId!=null){
            collect=infoMineMapper.selectByExample(Example.builder(InfoMine.class).where(Sqls.custom()
                    .andEqualTo("isDelete", false)
                    .andIsNull("parentId")
                    .andEqualTo("id",mineAreaId)).orderByDesc("id").build()).stream().map(x -> new RespCommon() {{
                setName(x.getMineName());
                setId(x.getId());
            }}).collect(Collectors.toList());
        }else{
            collect = allList.stream().filter(x->x.getParentId()==null).map(x -> new RespCommon() {{
                setName(x.getMineName());
                setId(x.getId());
            }}).collect(Collectors.toList());
        }
        collect.forEach(x->{
            x.setChildren(allList.stream().filter(y->x.getId().equals(y.getParentId())).map(z->new RespCommon(){{
                setId(z.getId());
                setName(z.getMineName());
            }}).collect(Collectors.toList()));
        });
        return MessageUtil.success(collect);
    }

    /**
     * 给评分作为入口的矿井列表
     *
     * @return
     */
    @Override
    public Message<List<RespMine>> getAllMineListByPf(Long mineId) {
        List<RespMine> list= infoMineMapper.getAllMineListByPf(mineId);
        return MessageUtil.success(list);
    }

    /**
     * 获取矿区矿井列表--根据上级ID
     *
     * @param belongId
     * @return
     */
    @Override
    public Message<List<RespMine>> getAllMineList(Long belongId) {
        List<RespMine> list= infoMineMapper.getAllMineListByPf(belongId);
        return MessageUtil.success(list);
    }


    /**
     * 修改矿井信息
     *
     * @param reqMineModify
     * @return
     */
    @Override
    public Message modify(ReqMineModify reqMineModify) {
        final RespUserInfo userInfo = JwtUtil.getUserInfo();
        final InfoMine infoMine = infoMineMapper.selectByPrimaryKey(reqMineModify.getId());
        if (infoMine == null) {
            return MessageUtil.error("获取数据失败!");
        }
        infoMine.setMineName(reqMineModify.getMineName());
        infoMine.setRemark(reqMineModify.getRemark());
        infoMine.setLatitude(reqMineModify.getLatitude());
        infoMine.setLongitude(reqMineModify.getLongitude());
        infoMine.setMineState(reqMineModify.getMineState());
        infoMine.setMineType(reqMineModify.getMineType());
        infoMine.setModifyId(userInfo.getId());
        infoMine.setModifyTime(DateUtil.date());
        infoMineMapper.updateByPrimaryKey(infoMine);
        return MessageUtil.success();
    }

    /**
     * 获取矿井详情
     *
     * @param id
     * @return
     */
    @Override
    public Message<ReqMineModify> getMineById(Long id) {
        final InfoMine infoMine = infoMineMapper.selectByPrimaryKey(id);
        if(infoMine==null){
            return MessageUtil.error("获取数据失败!");
        }
        ReqMineModify reqMineModify=new ReqMineModify();
        reqMineModify.setId(infoMine.getId());
        reqMineModify.setMineName(infoMine.getMineName());
        reqMineModify.setRemark(infoMine.getRemark());
        reqMineModify.setLatitude(infoMine.getLatitude());
        reqMineModify.setLongitude(infoMine.getLongitude());
        reqMineModify.setMineState(infoMine.getMineState());
        reqMineModify.setMineType(infoMine.getMineType());
        return MessageUtil.success(reqMineModify);
    }
}
